package com.friend.uu;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class MainActivity extends Activity {

    private static final String[] permissions = {
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.MODIFY_AUDIO_SETTINGS
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //保持竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        getPermissions();
    }

    private void getPermissions() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                int PMS_CODE = 1506;
                requestPermissions(permissions, PMS_CODE);
                return;
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.menu_about) {
            View v = getLayoutInflater().inflate(R.layout.text_selectable, null);
            TextView tv = v.findViewById(R.id.tv_selectable);
            tv.setText(R.string.about_content);
            new AlertDialog.Builder(this)
                    .setTitle(R.string.about)
                    .setView(v)
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
        } else if (itemId == R.id.menu_exit) {
            finish();
        }
        return true;
    }

    public void onMainUiClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_torch) {
            TorchUtils.torchToggle(this);
        } else if (id == R.id.btn_ruler_utils) {
            startActivity(new Intent(this, RulerActivity.class));
        } else if (id == R.id.btn_encrypt_utils) {
            startActivity(new Intent(this, EncryptActivity.class));
        } else if (id == R.id.btn_bluetooth_recorder) {
            startActivity(new Intent(this, BluetoothRecorderActivity.class));
        } else if (id == R.id.btn_p2p) {
            startActivity(new Intent(this, P2pActivity.class));
        }
    }
}
